package com.hclc.yxb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hclc.config.aop.log.OperationLogAnnotation;
import com.hclc.entity.dvo.AdministratorDvo;
import com.hclc.entity.model.contans.ApiConst;
import com.hclc.entity.model.page.PageUtil;
import com.hclc.entity.model.result.ResultEntity;
import com.hclc.yxb.ao.AddAdministratorAo;
import com.hclc.yxb.ao.ModAdministratorAo;
import com.hclc.yxb.dto.AdministratorDto;
import com.hclc.yxb.dto.AuthorityRoleDto;
import com.hclc.yxb.service.AdministratorManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @ClassName PwlAdministratorManagement
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 19:49 2020/8/22
 * @version: 1.0
 */
@Api(tags = "WEB-1.0-AdministratorManagement", description = "WEB-权限管理-管理员管理")
@RestController
@RequestMapping("console/admin")
@Validated
public class AdministratorManagementController {

    @Autowired
    private AdministratorManagementService administratorManagementService;

    /**
     * 查询管理员列表
     *
     * @param util
     * @return com.hclc.pwl.model.result.ResultEntity<com.baomidou.mybatisplus.core.metadata.IPage < com.hclc.pwl.dto.AdministratorDvo>>
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/22 21:06
     */
    @ApiOperation("查询管理员列表")
    @PostMapping(value = "/getAdministrator", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<IPage<AdministratorDto>> getAdministrator(@ApiParam(value = "分页信息") @RequestBody PageUtil util) {
        Page<AdministratorDvo> page = new Page<>(util.getCurrent(), ApiConst.PAGE_SIZE);
        return ResultEntity.ResultSuccess(administratorManagementService.selectAdministrator(page));
    }

    /**
     * 查询权限角色列表
     *
     * @param
     * @return com.hclc.pwl.model.result.ResultEntity<java.util.List < com.hclc.pwl.dto.AuthorityRoleDto>>
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/22 21:52
     */
    @ApiOperation("查询权限角色列表")
    @PostMapping(value = "/getAuthorityRole", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<List<AuthorityRoleDto>> getAuthorityRole() {
        return ResultEntity.ResultSuccess(administratorManagementService.selectAuthorityRole());
    }

    /**
     * 添加管理员
     *
     * @param ao
     * @return com.hclc.pwl.model.result.ResultEntity
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/22 20:10
     */
    @OperationLogAnnotation(remark = "添加管理员")
    @ApiOperation("添加管理员")
    @PostMapping(value = "/addAdministrator", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity addAdministrator(@ApiParam(value = "管理员信息") @Validated @RequestBody AddAdministratorAo ao) {
        return ResultEntity.ResultSuccess(administratorManagementService.insertAdministrator(ao));
    }

    /**
     * 删除管理员
     *
     * @param userId
     * @return com.hclc.pwl.model.result.ResultEntity
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/22 20:28
     */
    @OperationLogAnnotation(remark = "删除管理员")
    @ApiOperation("删除管理员")
    @GetMapping(value = "/delAdministrator", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity delAdministrator(@ApiParam(value = "用户id") @NotBlank(message = "用户id不能为空") @RequestParam String userId) {
        return ResultEntity.ResultSuccess(administratorManagementService.deleteAdministrator(userId));
    }

    /**
     * 修改管理员
     *
     * @param ao
     * @return com.hclc.pwl.model.result.ResultEntity
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/22 20:35
     */
    @OperationLogAnnotation(remark = "修改管理员")
    @ApiOperation("修改管理员")
    @PostMapping(value = "/modAdministrator", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity modAdministrator(@ApiParam(value = "管理员信息") @Validated @RequestBody ModAdministratorAo ao) {
        return ResultEntity.ResultSuccess(administratorManagementService.updateAdministrator(ao));
    }

    /**
     * 管理员修改自身登录密码
     *
     * @param password
     * @return com.hclc.pwl.model.result.ResultEntity
     * @author yurj
     * @mail 1638234804@qq.com
     * @version 1.0
     * @date 2020/8/22 20:35
     */
    @OperationLogAnnotation(remark = "管理员修改自身登录密码")
    @ApiOperation("管理员修改自身登录密码")
    @GetMapping(value = "/modCurrentAdministratorPass", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<Boolean> modCurrentAdministratorPass(@ApiParam(value = "管理员信息") @Validated @RequestParam String password) {
        return ResultEntity.ResultSuccess(administratorManagementService.updateCurrentAdministratorPass(password));
    }
}
